package despachador.pagina;

import excepciones.AvwException;

import gestores.pagina.GestorCliente;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.*;
import javax.servlet.http.*;

public class Registrar extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
    
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    
    public void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
   
        
        
        ArrayList<HashMap> paises = new ArrayList<HashMap>();
        ArrayList<HashMap> provincias = new ArrayList<HashMap>();
        
        String nombre = request.getParameter("nombre");
        String razonSocial = "";
        String direccion = request.getParameter("direccion");
        String piso = request.getParameter("piso");
        String depto= request.getParameter("depto");
        String apellido = request.getParameter("apellido");
        String pais =  request.getParameter("pais");
        String provincia = request.getParameter("provincia");
        String localidad = request.getParameter("localidad");
        String cp = request.getParameter("cp");
        String tipoiva = request.getParameter("tipoIva");
        String cuit = request.getParameter("cuit");
        String email = request.getParameter("email");
        String contrasenia1 = request.getParameter("contrasenia1");
        String contrasenia2 = request.getParameter("contrasenia2");
        String opcion ="";
        if(request.getParameter("opcion")!= null){
            opcion = (String)request.getParameter("opcion");
        }

        try{
            if(opcion.equals("crear")){
                if (email != null && !email.equals("")){
                    if (!GestorCliente.insertarCliente(nombre, razonSocial,
                            direccion, piso,
                            depto, pais,
                            provincia,
                            localidad,
                            cp, tipoiva,
                            cuit, email,
                            contrasenia1)) {
                        // Utilizo la variable msg para saber si el email ya existe o no
                        //si existe msg es true
                        request.setAttribute("_msg","true");
                        armarPagina(request,response);
                    }else{
                        String direc = "";
                        if( request.getSession(true).getAttribute("atributosUrl")!=null){
                            direc = (String) request.getSession(true).getAttribute("atributosUrl");
                        }
                        response.sendRedirect("./index?" + direc );

                    }//fin else
                }else{
                    armarPagina(request,response);
                }
            }else{
                armarPagina(request,response);
            }
        }catch (Exception e) {
            request.setAttribute("error",
                    e.getMessage());
        }//fin catch
        
        
    }
    
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    
    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    
    private void armarPagina(HttpServletRequest request,
            HttpServletResponse response){
                 String url = "/micrositios/" + (String) request.getAttribute("nomMicro");
                // Especificamos que pagina se coloca en cada parte del layout
                request.setAttribute("1", url + "/encabezado.jsp");
                request.setAttribute("2", url + "/destacado.jsp");
                request.setAttribute("3", url + "/buscador.jsp");
                request.setAttribute("4", "/vistas/pagina/menuizq.jsp");
                request.setAttribute("5", url + "/bannerizq.jsp");

                request.setAttribute("6", "/vistas/pagina/registrar.jsp");

                request.setAttribute("7", "/vistas/pagina/visitados.jsp");
                request.setAttribute("8", "/vistas/pagina/login.jsp");
                request.setAttribute("9", "/vistas/pagina/rss.jsp");
                request.setAttribute("11", "/vistas/pagina/marcas.jsp");
                request.setAttribute("10", url + "/pie.jsp");

                try {

                    dispatch(request, response, "/compositor/pagina/nuevecuerpos.jsp");
                } catch (Exception ex) {
                    request.setAttribute("error",
                    ex.getMessage());
                } 
    }

    /**
     * dispatcher method
     */
    protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page)
            throws javax.servlet.ServletException,
            java.io.IOException {
        RequestDispatcher dispatcher =
                getServletContext().
                getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }
}
